#include "environment.h"
#include "app.h"
#include "FreeRTOS.h"
#include "SparkFunBME280.h"
BME280 bme280;
// #include "SparkFunCCS811.h"
// CCS811 mySensor(0x5A);


static lv_obj_t *scr;
lv_chart_series_t * ser1;

static lv_task_t * task;


 
lv_obj_t * item_create(lv_obj_t *par,const void *img_src, const char *name, const char *value_str)
{
    lv_obj_t *temp = lv_cont_create(par,NULL);
    lv_obj_set_size(temp,176,(264-40)/4);
    //lv_cont_set_fit2(temp, LV_FIT_NONE, LV_FIT_NONE);

     

    lv_obj_t *img = lv_img_create(temp,NULL);
    
    lv_img_set_src(img,img_src);


    lv_obj_t *label = lv_label_create(temp,NULL);
    lv_label_set_text(label,name);

    lv_obj_t *val = lv_label_create(temp,NULL);
    lv_label_set_text(val,value_str);

    lv_obj_align(img,temp,LV_ALIGN_IN_LEFT_MID,0,0);
    lv_obj_align(label,img,LV_ALIGN_OUT_RIGHT_MID,0,10);
    lv_obj_align(val,temp,LV_ALIGN_IN_RIGHT_MID,0,-20);

    return temp;

}

static lv_obj_t *label_pressure;
static lv_obj_t *label_temper;
static lv_obj_t *label_humidity;
static lv_obj_t *chart_temper;
static void my_task(lv_task_t * task);


static void btn_back_event_cb(lv_obj_t * btn, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {
        lv_obj_t * par = lv_obj_get_parent(btn);
        lv_obj_del(par);
         
        lv_label_set_text(label_menu,"MENU");
        lv_obj_align(label_menu,scr_tft,LV_ALIGN_CENTER, 0 ,0);
        if(chart_temper)
        {
            lv_obj_del(chart_temper);
        }
        

        eink_scene_ready=false;
        eink_flash_ready=false;


        lv_task_del(task);
    }
}

static void temper_chart_event(lv_obj_t * btn, lv_event_t event)
{
    if(event == LV_EVENT_CLICKED) {
        // lv_chart_set_series_opa(chart_temper,LV_OPA_100);
        Serial.println("-------clicked----");
    }
    
}


void environment_create(lv_obj_t *obj, lv_event_t event)
{
    
 
    if(event==LV_EVENT_CLICKED)
    {

        // CCS811Core::status returnCode = mySensor.begin();
        // if (returnCode != CCS811Core::SENSOR_SUCCESS)
        // {
        //     Serial.println(".begin() returned with an error.");
        //     while (1); //Hang if there was a problem.
        // }
        //  xTaskCreatePinnedToCore(
        //                             sensor_loop
        //                             ,  "senor_loop"   // A name just for humans
        //                             ,  4096  // This stack size can be checked & adjusted by reading the Stack Highwater
        //                             ,  NULL
        //                             ,  1  // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
        //                             ,  NULL 
        //                             ,  1);

        bme280.setI2CAddress(0x76);
        if (bme280.beginI2C() == false) //Begin communication over I2C
        {
            Serial.println("The sensor did not respond. Please check wiring.");
            while(1); //Freeze
        }
        

        int v_humidity = int(bme280.readFloatHumidity());
        int v_pressure = int(bme280.readFloatPressure());
        int v_temper = int(bme280.readTempC());
        
        char buffer[10];

        
        


        

        //tft
        lv_label_set_text( label_menu,"Environment");
        lv_obj_align(label_menu,scr_tft,LV_ALIGN_CENTER,0,0);

        // chart_temper = lv_chart_create(scr_tft,NULL);
        // lv_obj_set_size(chart_temper,160,80);
        // lv_chart_set_type(chart_temper, LV_CHART_TYPE_LINE);   /*Show lines and points too*/
        // lv_chart_set_series_opa(chart_temper, LV_OPA_70);                            /*Opacity of the data series*/
        // lv_chart_set_series_width(chart_temper, 2);                                  /*Line width and point radious*/

        // lv_chart_set_range(chart_temper, 0, 45);
        // /*Add two data series*/
        // ser1 = lv_chart_add_series(chart_temper, LV_COLOR_RED);
        // lv_chart_set_series_opa(chart_temper,LV_OPA_0);

         
        //eink
       
        scr = lv_obj_create(scr_eink,NULL);
        lv_obj_set_size(scr,176,264);


        btn_back_create(scr,btn_back_event_cb);
        // lv_obj_t *cont = page_create(scr);


        //LV_FONT_DECLARE(font_digifor_48);
        LV_IMG_DECLARE(img_c_24);
        LV_IMG_DECLARE(img_percent_24);

        uint8_t pos_x=10;
        uint8_t pos_y=10;
        //temperature
        label_temper = lv_label_create(scr,NULL);
        static lv_style_t style_num;
        lv_style_copy(&style_num, &lv_style_plain);       
        style_num.text.font = &lv_font_roboto_28;
        lv_label_set_style(label_temper,LV_LABEL_STYLE_MAIN,&style_num);

        snprintf(buffer,sizeof(buffer),"%d",v_humidity);
        lv_label_set_text(label_temper,buffer);
        lv_obj_set_pos(label_temper,pos_x,pos_y);

        lv_obj_t *img_c = lv_imgbtn_create(scr,NULL);
        lv_imgbtn_set_src(img_c,LV_BTN_STATE_REL,&img_c_24);
        lv_obj_align(img_c,label_temper,LV_ALIGN_OUT_RIGHT_TOP,0,0);         
        lv_obj_set_event_cb(img_c,temper_chart_event);


        //humidity
        label_humidity = lv_label_create(scr,label_temper);
        memset(buffer,0,10);
        snprintf(buffer,sizeof(buffer),"%d",v_temper);
        lv_label_set_text(label_humidity,buffer);
        lv_obj_set_pos(label_humidity,pos_x,pos_y+40*1);

        lv_obj_t *img_humidity = lv_img_create(scr,NULL);
        lv_img_set_src(img_humidity,&img_percent_24);
        lv_obj_align(img_humidity,label_humidity,LV_ALIGN_OUT_RIGHT_TOP,0,0);


        //pressure
        label_pressure = lv_label_create(scr,label_temper);
        memset(buffer,0,10);
        snprintf(buffer,sizeof(buffer),"%d",v_pressure);
        lv_label_set_text(label_pressure,buffer);
        lv_obj_set_pos(label_pressure,pos_x,pos_y+40*2);


        
        lv_obj_t *label_co2 = lv_label_create(scr,label_temper);
        lv_label_set_text(label_co2,"1400");
        lv_obj_set_pos(label_co2,pos_x,pos_y+40*3);

        lv_obj_t *label_tvoc = lv_label_create(scr,label_temper);
        lv_label_set_text(label_tvoc,"300");
        lv_obj_set_pos(label_tvoc,pos_x,pos_y+40*4);



        // LV_IMG_DECLARE(img_co2_48);
        // LV_IMG_DECLARE(img_humidity_48);
        // LV_IMG_DECLARE(img_temperature_48);
        // LV_IMG_DECLARE(img_pressure_48);

        // lv_obj_t *temperature = item_create(cont,&img_temperature_48,"Temperature","22");
        // lv_obj_t *humidity = item_create(cont,&img_humidity_48,"humidity","40");
        // lv_obj_t *pressure = item_create(cont,&img_pressure_48,"pressure","2700");
        // lv_obj_t *co2 = item_create(cont,&img_co2_48,"co2","498");
        
        // lv_obj_set_pos(temperature,0,0);
        // lv_obj_align(humidity,temperature,LV_ALIGN_OUT_BOTTOM_MID,0,0);
        // lv_obj_align(pressure,humidity,LV_ALIGN_OUT_BOTTOM_MID,0,0);
        // lv_obj_align(co2,pressure,LV_ALIGN_OUT_BOTTOM_MID,0,0);

        eink_scene_ready=false;
        eink_flash_ready=false;
        //task
         task = lv_task_create(my_task, 1000, LV_TASK_PRIO_LOWEST, NULL);
         
         lv_task_set_period(task,1000*5);


    }
}

static void my_task(lv_task_t * task)
{
    float v_humidity = bme280.readFloatHumidity();
    Serial.print("Humidity: ");
    Serial.print(v_humidity, 0);
    char buffer[10];
    snprintf(buffer,sizeof(buffer),"%d",int(v_humidity));
    lv_label_set_text(label_humidity,buffer);
    Serial.println(buffer);

    float v_pressure = bme280.readFloatPressure();
    Serial.print(" Pressure: ");
    Serial.print(v_pressure, 0);
    memset(buffer,0,10);
    snprintf(buffer,sizeof(buffer),"%d",int(v_pressure));
    lv_label_set_text(label_pressure,buffer);


    Serial.print(" Alt: ");
    Serial.print(bme280.readFloatAltitudeMeters(), 1);
    // Serial.print(bme280.readFloatAltitudeFeet(), 1);


    float v_temper = bme280.readTempC();
    Serial.print(" Temp: ");
    Serial.print(v_temper, 2);
    // Serial.print(bme280.readTempF(), 2);
    memset(buffer,0,10);
    snprintf(buffer,sizeof(buffer),"%d",int(v_temper));
    lv_label_set_text(label_temper,buffer);
    Serial.println(buffer);

    Serial.println();


    // lv_chart_set_next(chart_temper,ser1,int(v_temper));


    eink_scene_ready=false;
    eink_flash_ready=false;

 

   
   

}






